package com.dqhl.community.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.activity.ApplianceCleanActivity;
import com.dqhl.community.activity.ApplianceRepairActivity;
import com.dqhl.community.activity.CommunityNoticeActivity;
import com.dqhl.community.activity.ExpressReceiveActivity;
import com.dqhl.community.activity.FamilyServiceActivity;
import com.dqhl.community.activity.HospitalActivity;
import com.dqhl.community.activity.LoginActivity;
import com.dqhl.community.activity.PropertyPayActivity;
import com.dqhl.community.activity.ShoppingAroundActivity;
import com.dqhl.community.activity.ShoppingOnlineActivity;
import com.dqhl.community.activity.SpeciaPagelBarActivity;
import com.dqhl.community.activity.SquareDancingActivity;
import com.dqhl.community.activity.YingYangPinActivity;
import com.dqhl.community.base.BaseFragment;
import com.dqhl.community.util.Constant;

/**
 * Created by guanluocang on 2016/8/29.
 * 首页 fragment
 */
public class HomePageFragment extends BaseFragment implements View.OnClickListener {

    private TextView tv_login;
    private TextView tv_homeTitle;
    private ImageView iv_information;//消息
    private ImageView iv_communityotice_two;//社区公告Activity
    private ImageView iv_applianceClear_two;//家电清洗Activity
    private ImageView iv_applianceRepair_two;//家电维修Activity
    private ImageView iv_hospital_two;//社区医疗
    private ImageView iv_kuaidiReceiver;//快递代收Activity
    private ImageView iv_areaShopping_two;//周边购物Activity
    private ImageView iv_houseKeeping;//家政服务
    private LinearLayout ll_property; //物业缴费
    private LinearLayout ll_community; //社区公告
    private LinearLayout ll_hosipital; //社区医疗
    private LinearLayout ll_shopping; //在线超市
    private ImageView mIvBar; //专题啤酒节
    private ImageView mIvGuangChang; //专题广场舞
    private ImageView mIvYingYang; //专题营养品

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_page, container, false);

        initView(view);
        initListener();
        initData();
        registerBroadcastReceiver();
        return view;

    }

    /**
     * 初始化控件
     *
     * @param view view
     */
    private void initView(View view) {
        tv_login = (TextView) view.findViewById(R.id.tv_top_login);
        iv_information = (ImageView) view.findViewById(R.id.iv_top_operation);
        tv_homeTitle = (TextView) view.findViewById(R.id.tv_top_title);
        iv_hospital_two = (ImageView) view.findViewById(R.id.iv_hospital_two);
        iv_kuaidiReceiver = (ImageView) view.findViewById(R.id.iv_kuaidiReceiver);
        iv_applianceClear_two = (ImageView) view.findViewById(R.id.iv_applianceClear_two);
        iv_applianceRepair_two = (ImageView) view.findViewById(R.id.iv_applianceRepair_two);
        iv_communityotice_two = (ImageView) view.findViewById(R.id.iv_communityotice_two);
        iv_houseKeeping = (ImageView) view.findViewById(R.id.iv_houseKeeping);
        iv_areaShopping_two = (ImageView) view.findViewById(R.id.iv_areaShopping_two);
        ll_property = (LinearLayout) view.findViewById(R.id.ll_property);
        ll_community = (LinearLayout) view.findViewById(R.id.ll_community);
        ll_hosipital = (LinearLayout) view.findViewById(R.id.ll_hosipital);
        ll_shopping = (LinearLayout) view.findViewById(R.id.ll_shopping);
        mIvBar = (ImageView) view.findViewById(R.id.iv_bar_item);
        mIvGuangChang = (ImageView) view.findViewById(R.id.iv_guangchang_item);
        mIvYingYang = (ImageView) view.findViewById(R.id.iv_yingyang_item);

    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        tv_login.setOnClickListener(this);
        tv_login.setText(R.string.login);
        tv_login.setVisibility(View.VISIBLE);
        iv_information.setVisibility(View.VISIBLE);
        tv_homeTitle.setText(R.string.main_title);
        iv_information.setOnClickListener(this);
        iv_hospital_two.setOnClickListener(this);
        iv_kuaidiReceiver.setOnClickListener(this);
        iv_applianceClear_two.setOnClickListener(this);
        iv_applianceRepair_two.setOnClickListener(this);
        iv_communityotice_two.setOnClickListener(this);
        iv_houseKeeping.setOnClickListener(this);
        iv_areaShopping_two.setOnClickListener(this);
        ll_property.setOnClickListener(this);
        ll_community.setOnClickListener(this);
        ll_hosipital.setOnClickListener(this);
        ll_shopping.setOnClickListener(this);
        mIvBar.setOnClickListener(this);
        mIvGuangChang.setOnClickListener(this);
        mIvYingYang.setOnClickListener(this);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        String strUsername = spUserInfo.getString(Constant.USER_PHONE, "");
        if (!"".equals(strUsername) && strUsername.length() > 0) {
            tv_login.setVisibility(View.GONE);
        } else {
            tv_login.setVisibility(View.VISIBLE);
            tv_login.setText(R.string.login);
            tv_login.setClickable(true);
        }

    }

    /**
     * 定义 BroadcastReceiver
     */
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case Constant.ACTION_LOGIN_SUCCESS: // 登录成功，隐藏登录按钮
                    tv_login.setVisibility(View.GONE);
                    break;
                case Constant.ACTION_LOGOUT: //退出登录，显示登录按钮
                    tv_login.setVisibility(View.VISIBLE);
                    tv_login.setText(R.string.login);
                    tv_login.setClickable(true);
                    break;
            }
        }
    };

    /**
     * 注册广播
     */
    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACTION_LOGIN_SUCCESS);
        intentFilter.addAction(Constant.ACTION_LOGOUT);
        getActivity().registerReceiver(receiver, intentFilter);
    }

    //控件的点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //登录按钮
            case R.id.tv_top_login:
                overlay(LoginActivity.class);
                break;
            //消息
            case R.id.iv_top_operation:
                toast("暂无消息");
                break;
            //物业缴费
            case R.id.ll_property:
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(PropertyPayActivity.class);
                }
                break;
            //社区公告
            case R.id.ll_community:
            case R.id.iv_communityotice_two: //社区公告（下）
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(CommunityNoticeActivity.class);
                }
                break;
            //家电维修（下）
            case R.id.iv_applianceRepair_two:
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else {
                    overlay(ApplianceRepairActivity.class);
                }
                break;
            //家电清洗
            case R.id.iv_applianceClear_two: //家电清洗（下）
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else {
                    overlay(ApplianceCleanActivity.class);
                }
                break;
            //消息
            case R.id.tv_top_operation:
                toast("消息");
                break;
            //社区医疗
            case R.id.ll_hosipital:
            case R.id.iv_hospital_two://社区医疗（下）
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(HospitalActivity.class);
                }
                break;
            //在线超市
            case R.id.ll_shopping:
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(ShoppingOnlineActivity.class);
                }
                break;
            //快递代收
            case R.id.iv_kuaidiReceiver:
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(ExpressReceiveActivity.class);
                }
                break;
            //家政服务
            case R.id.iv_houseKeeping:
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else {
                    overlay(FamilyServiceActivity.class);
                }
                break;
            //周边购物
            case R.id.iv_areaShopping_two:
                if ("".equals(app.getUserId()) || app.getUserId() == null) {
                    toast("请先登录");
                } else if ("".equals(app.getAuthentication()) || app.getAuthentication() == null) {
                    toast(R.string.toast_go_to_auth);
                } else {
                    overlay(ShoppingAroundActivity.class);
                }
                break;
            //专题啤酒节
            case R.id.iv_bar_item:
                overlay(SpeciaPagelBarActivity.class);
                break;
            //专题广场舞
            case R.id.iv_guangchang_item:
                overlay(SquareDancingActivity.class);
                break;
            //专题营养品
            case R.id.iv_yingyang_item:
                overlay(YingYangPinActivity.class);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 注销广播
        if (receiver != null) {
            getActivity().unregisterReceiver(receiver);
        }
    }

}
